package data_structure.stack;

import lombok.Data;

import java.util.Arrays;

/**
 * @ClassName : data_structure.stack.Calculator
 * @Description : 类描述
 * Created by cyd on 2021-02-01 18:38:41
 */
public class Calculator {
    public static void main(String[] args) {
        String express = "100+2*3+2*2";
        ArrayStack2 numStack = new ArrayStack2(10);
        ArrayStack2 operStack = new ArrayStack2(10);
        // 定义需要扫描的相关变量
        int index = 0;
        int num1 = 0;
        int num2 = 0;
        int oper = 0;
        int result = 0;
        char ch = ' '; // 用于储存每次扫描得到的结果
        StringBuffer keepNum = new StringBuffer("");// 用于拼接多位数的字符

        // 计算到最后，数栈中只剩最后一个数，就是结果，符号栈为空
        while (true) {
            // 依次得到每一个字符
            ch = express.substring(index, index + 1).charAt(0);
            // 判断ch
            if (operStack.isOper(ch)) {
                if (!operStack.isEmpty()) {
                    // 如果不为空
                    if (operStack.priority(ch) <= operStack.priority(operStack.peek())) {
                        // 从数栈中抛出弹出两个数做运算
                        num1 = numStack.pop();
                        num2 = numStack.pop();
                        oper = operStack.pop();
                        result = numStack.cal(num1, num2, oper);
                        // 把运算结果入数栈
                        numStack.push(result);
                        // 把当前的操作符入符号栈
                        operStack.push(ch);
                    } else {
                        // 如果当前ch的优先级更大，就直接入符号栈
                        operStack.push(ch);
                    }
                } else {
                    // 如果为空
                    operStack.push(ch);

                }
            } else {
                // 如果ch是express的最后一位，直接入数栈
                if (index == express.length() - 1) {
                    numStack.push(ch - 48);
                } else {
                    keepNum.append(ch);
                    if (operStack.isOper(express.substring(index + 1, index + 2).charAt(0))) {
                        numStack.push(Integer.parseInt(keepNum.toString()));
                        keepNum.setLength(0);
                    }
                }
            }
            index++;
            // 判断是否扫描完毕
            if (index >= express.length()) break;
        }

        while (true) {
            if (operStack.isEmpty()) break;
            // 从数栈中抛出弹出两个数做运算
            num1 = numStack.pop();
            num2 = numStack.pop();
            oper = operStack.pop();
            result = numStack.cal(num1, num2, oper);
            numStack.push(result);
        }

        System.out.printf("表达式%s = %d", express, numStack.pop());


    }
}

@Data
class ArrayStack2 {
    private Integer maxSize;
    private Integer[] stack;
    private Integer top = -1;

    public ArrayStack2(Integer maxSize) {
        this.maxSize = maxSize;
        this.stack = new Integer[maxSize];
    }

    // 判断栈为空
    public boolean isEmpty() {
        return this.top == -1;
    }

    // 判断栈是否已满
    public boolean isFull() {
        return this.top == maxSize - 1;
    }

    // 向栈中添加数据
    public void push(int n) {
        if (this.isFull()) {
            System.out.println("此栈已满，不能压入");
            return;
        }
        top = top + 1;
        stack[top] = n;
    }

    // 向栈中弹出数据
    public Integer pop() {
        if (this.isEmpty()) {
            System.out.println("此栈已空，不能弹出");
            return null;
        }
        Integer value = stack[top];
        stack[top] = null;
        top = top - 1;
        return value;
    }

    // peek方法
    public int peek() {
        return stack[top];
    }

    // 输出栈
    public void show() {
        System.out.println(Arrays.toString(this.stack));
    }

    // 判断输入的符号的优先级
    public int priority(int oper) {
        if (oper == '*' || oper == '/') {
            return 1;
        } else if (oper == '+' || oper == '-') {
            return 0;
        } else {
            return -1;// 传入的符号有问题
        }
    }

    // 判断是不是一个符号
    public boolean isOper(char val) {
        return val == '+' || val == '-' || val == '*' || val == '/';
    }

    //计算方法
    public int cal(int num1, int num2, int oper) {
        int result = 0;
        switch (oper) {
            case '+':
                result = num1 + num2;
                break;
            case '-':
                result = num1 - num2;
                break;
            case '*':
                result = num1 * num2;
                break;
            case '/':
                result = num1 / num2;
                break;
        }

        return result;
    }

}